package com.bilicraft;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import res.Resources;

public class MButton extends JPanel implements MouseListener, MouseMotionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public Resources res;
	public String title;
	public int msts;
	public boolean enable;
	
	public MButton(String t, Resources rs, int x, int y){
		res = rs;
		title = t;
		msts = 0;
		setSize(200, 40);
		setLocation(x, y);
		addMouseListener(this);
		addMouseMotionListener(this);
		enable = true;
	}
	
	public void paintComponent(Graphics g){
		switch(msts){
		case 0:
			g.drawImage(res.butt, 0, 0, this);
			break;
		case 1:
			g.drawImage(res.buth, 0, 0, this);
			break;
		case 2:
			g.drawImage(res.butd, 0, 0, this);
			break;
		}
		g.setColor(Color.BLACK);
		g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
		FontMetrics fm = g.getFontMetrics();
		int stringWidth = fm.stringWidth(title);                            
		int stringAscent = fm.getAscent();                                 
		int stringDescent = fm.getDescent(); 
		int x = this.getWidth() / 2 - stringWidth / 2;                
		int y = this.getHeight() / 2 + (stringAscent - stringDescent) / 2;          
		g.drawString(title, x+1, y+1);
		if(enable)
			g.setColor(Color.WHITE);
		else
			g.setColor(Color.LIGHT_GRAY);
		g.drawString(title, x-1, y-1);
	}
	
	public void mouseExited(MouseEvent e){
		if(!enable)
			return;
		if(msts == 1){
			msts = 0;
			repaint();
		}
		mExited(e);
		e.consume();
	}
	public void mouseReleased(MouseEvent e){	
		//System.out.println(this+"Released...");
		if(!enable)
			return;
		msts = 1;
		repaint();
		mReleased(e);
		e.consume();
	}
	public void mouseClicked(MouseEvent e){		
		if(!enable)
			return;
		//System.out.println(this+"Clicked...");
		mClicked(e);
		e.consume();
	}
	public void mousePressed(MouseEvent e){	
		if(!enable)
			return;
		//System.out.println(this+"Pressed...");
		msts = 2;
		repaint();
		mPressed(e);
		e.consume();
	}
	public void mouseEntered(MouseEvent e){
		if(!enable)
			return;
		if(msts != 1){
			msts = 1;
			repaint();
		}
		mEntered(e);
		e.consume();
	}
	public void mouseMoved(MouseEvent e){	
		if(!enable)
			return;
		mMoved(e);
		e.consume();
	}
	public void mouseDragged(MouseEvent e){	
		if(!enable)
			return;
		mDragged(e);
		e.consume();
	}
	
	public void mExited(MouseEvent e){		
	}	
	public void mReleased(MouseEvent e){		
	}	
	public void mClicked(MouseEvent e){		
	}
	public void mPressed(MouseEvent e){
	}
	public void mEntered(MouseEvent e){		
	}	
	public void mMoved(MouseEvent e){		
	}
	public void mDragged(MouseEvent e){
	}
}
